package com.jky.stmall.front.mapper;

import com.jky.stmall.front.entity.Cart;
import org.apache.ibatis.annotations.*;

import java.util.List;

//购物车的操作
@Mapper
public interface CartMapper {
    //添加
    @Insert({"insert into cart",
            "(count,goods_id,user_id)",
            "value(#{count},#{goodsId},#{userId})"
    })
    int insert(Cart cart);
    //删除
    @Delete("delete from cart where id =#{id}")
    int delete(Integer id);
    //修改
    @Update({"update cart",
            "set count=#{count}",
            "where id=#{id}"
    })
    int update(Cart cart);

    //查询
    @Select({"select * from cart",
            "where user_id=#{userId}"
    })
    @Results(id = "cart",value = {
            @Result(column = "goods_id",property = "goodsId"),
            @Result(column = "goods_id",property = "goods",
            one = @One(select = "com.jky.stmall.front.mapper.GoodsMapper.selectById"))
    })
    List<Cart> selectByUserId(Integer userId);


    @Select("select * from cart where id=#{id}")
    Cart selectById(Integer id);

    @Select("select * from cart where goods_id=#{goodsId} and user_id=#{userId}")
    Cart selectByGoodsIdAndUserId(@Param("goodsId") Integer goodsId,@Param("userId") Integer userId);
//根据多个ID查询购物车信息
    @Select({
            "<script>",
            "select * from cart",
            "where id in",
            "<foreach collection='cartIds' item ='cartId' open='(' close=')' separator=','>",
            "#{cartId}",//(1,2,3,4)
            "</foreach>",
            "</script>",
    })
    @ResultMap("cart")
    List<Cart> selectByIds(@Param("cartIds")Integer[] cartIds);
}
